/*
 * $Id: FileLineCounterTests.groovy 5 2008-12-10 03:04:30Z marcos.silva $
 *
 * Copyright 2008 Marcos Silva Pereira
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */
package com.google.code.grailssloc

/**
 * @author Marcos Silva Pereira
 */
public class FileLineCounterTests extends GroovyTestCase {
	 
	 FileLineCounter counter
	 
	 protected void setUp() {
		 super.setUp()
		 counter = new FileLineCounter()
	 }
	 
	 public void testMustRecognizeSingleLineComments() {
		 assertTrue counter.singleLineComment('// a single line comment')
	 }
	 
	 public void testEmptyLinesAreNotSingleLineComments() {
		 assertFalse counter.singleLineComment('')
	 }
	 
	 public void testMustRecognizeSingleLineCommentsWhenStringStartsWithSpaces() {
		 assertTrue counter.singleLineComment('    // a comment')
	 }
	 
	 public void testAvoidSingleCommentsWhenTheyAreInTheEndOfTheLine() {
		 assertFalse counter.singleLineComment(' int a; // a variable')
	 }
	 
	 public void testRecognizeAMultiLineComment() {
		 assertTrue counter.multiLineComment('/*')
	 }
	 
	 public void testRecognizeAMultiLineCommentWhenItStartsAsAJavadocComment() {
		 assertTrue counter.multiLineComment('/**')
	 }
	 
	 public void testRecognizeAMultiLineCommentWhenItIsNotInTheLineStart() {
		 assertTrue counter.multiLineComment('       /*')
		 assertTrue counter.multiLineComment('int some; /*')
		 assertTrue counter.multiLineComment('int some; /**')
	 }
	 
	 public void testRecognizeEndOfMultiLineComment() {
		 assertTrue counter.endMultiLineComment('*/')
	 }
	 
	 public void testRecognizeEndOfCommentWhenItIsNotInTheEndOfTheLine() {
		 assertTrue counter.endMultiLineComment('*/ int some;')
	 }
	 
	 public void testRecognizeEndOfLineCommentWhenItIsTheMiddleOfLines() {
		 assertTrue counter.endMultiLineComment(' still a comment. */ int some;')
	 }
	 
	 public void testRecognizeABlankLine() {
		 assertTrue counter.blankLine('')
	 }
	 
	 public void testRecognizeBlankLinesWhenTheyHasEmptySpaces() {
		 assertTrue counter.blankLine('          ')
		 assertTrue counter.blankLine('\n       \t')
	 }
	 
	 public void testMustCountCodeLines() {
		 def result = counter.countLinesFor('''
				 int a = 10;
		 		 int b = 50;

		 		 10.times {
		 			 println it
		 		 }
				 ''')
				 
		assertEquals 5, result.codeLines
		assertEquals 1, result.blankLines
		assertEquals 6, result.totalLines
	 }
	 
	 public void testMustCountCommentLines() {
		 def result = counter.countLinesFor('''
				 // declaring a variable
				 int a = 20 // ops, you should not count with me
				 /*
				 now i am a multiline comment
				 and you should count
				 more than one line
				 */
				 int b = 10

				 /**
				  * I am some kind of javadoc comment
				  * please, count my lines
				  * correctly
				  */
				  10.times { // empty }


		 		 // final comment line
				 ''')
				 
		 assertEquals 12, result.commentLines
		 assertEquals 3 , result.codeLines
	 }
	 
	 public void testMustCountLinesFromAFile() {
		 
		 def file = new File('/home/eu/workspace3.4/grails-sloc/src/groovy/com/google/code/grailssloc/CounterResults.groovy')
		 
		 def result = counter.countLinesFor(file.text)
		 
		 println result.codeLines
		 println result.commentLines
		 println result.blankLines
		 println result.totalLines
		 
	 }
	 
	 protected void tearDown() {
		 super.tearDown()
		 counter = null
	 }
	 
}
